import { constantRoutes, asyncRoutes } from '@/router/index'

export default {
  namespaced: true,
  state: {
    routes: constantRoutes // 该用户的路由映射，初始为静态，后面再添加
  },
  getters: {},
  mutations: {
    setRoutes(state, data) {
      state.routes = [...constantRoutes, ...data] // 静态加动态
    }
  },
  actions: {
    // 根据后端返回的权限标识和本地完整的动态路由数组进行筛选，筛选出属于当前用户的动态路由
    filterRoutes(context, data) {
      const userAsyncRoutes = []
      //   console.log(data)
      // 判断item.name是否在该用户的权限标识中
      asyncRoutes.forEach(item => {
        if (data.includes(item.name)) {
          userAsyncRoutes.push(item)
        }
      })
      //   把*通配的404界面放在动态路由的最后面
      userAsyncRoutes.push({ path: '*', redirect: '/404', hidden: true })
      //   调用设置路由信息
      context.commit('setRoutes', userAsyncRoutes)
      //   console.log(userAsyncRoutes)
      return userAsyncRoutes
    }
  }
}
